//
//  HWContentVideoView.m
//  JWC
//
//  Created by lanou on 16/4/11.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWContentVideoView.h"
#import "HWDetailPictureController.h"
#import "KRVideoPlayerController.h"
#import <AVFoundation/AVFoundation.h>
#import "UIImageView+WebCache.h"

@interface HWContentVideoView ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UILabel *playCount;

@property (weak, nonatomic) IBOutlet UILabel *playTime;

@property (weak, nonatomic) IBOutlet UIButton *playBtn;

@property (nonatomic, strong)KRVideoPlayerController *videoController;

@end

@implementation HWContentVideoView

- (void)awakeFromNib
{
    self.autoresizingMask = UIViewAutoresizingNone;
    self.imageView.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPicture)]];
}

+(instancetype)videoView {
    
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

-(void)showPicture {
    HWDetailPictureController *showPicVc = [[HWDetailPictureController alloc]init];
    showPicVc.model = self.model;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPicVc animated:YES completion:nil];
}

- (void)setModel:(HWRecommendModel *)model
{
    _model = model;
    self.playCount.text = [NSString stringWithFormat:@"%ld播放", (long)model.video.playcount];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.video.thumbnail.firstObject]];
    self.playTime.text = [NSString stringWithFormat:@"%02ld:%02ld", (long)model.video.duration / 60, (long)model.video.duration % 60];
}

- (IBAction)playButton:(UIButton *)sender
{
    [self playVideoWithURL:[NSURL URLWithString:self.model.video.video.firstObject]];
    [self addSubview:self.videoController.view];
}

- (void)playVideoWithURL:(NSURL *)url
{
    if (!self.videoController) {
        self.videoController = [[KRVideoPlayerController alloc] initWithFrame:self.imageView.bounds];
        __weak typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
        }];
    }
    self.videoController.contentURL = url;
}

// 停止播放视频
- (void)reset
{
    [self.videoController dismiss];
    self.videoController = nil;
}

@end































